package org.lc.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.lc.gateway.context.ContextExtraDataGenerator;
import org.lc.gateway.filter.*;
import org.lc.gateway.properties.GatewayPluginProperties;
import org.lc.platform.redis.service.CacheService;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.stream.Collectors;

/**
 * Gateway Plugin Config
 *
 * @author dl
 * 2023/01/29
 */
@Slf4j
@Configuration
public class GatewayPluginConfig {

    /**
     * Gateway插件是否生效
     *
     * @return {}
     */
    @Bean
    @ConditionalOnMissingBean(GatewayPluginProperties.class)
    public GatewayPluginProperties gatewayPluginProperties() {
        return new GatewayPluginProperties();
    }

    /**
     * sql注入拦截插件
     *
     * @return {}
     */
    @Bean
    @ConditionalOnMissingBean(SqlInjectionFilter.class)
    public SqlInjectionFilter sqlInjectionFilter(@Autowired GatewayPluginProperties gatewayPluginProperties) {
        return new SqlInjectionFilter(gatewayPluginProperties);
    }

    /**
     * sql注入拦截插件
     *
     * @return {}
     */
    @Bean
    @ConditionalOnMissingBean(XssInjectionFilter.class)
    public XssInjectionFilter xssInjectionFilter(@Autowired GatewayPluginProperties gatewayPluginProperties) {
        return new XssInjectionFilter(gatewayPluginProperties);
    }

    /**
     * 缓存请求消息插件是否生效
     * logRequest sqlInjection xssInjection 其中任何一个插件生效即可
     *
     * @return {}
     */
    @Bean
    @ConditionalOnBean(GatewayPluginProperties.class)
    @ConditionalOnMissingBean(GatewayRequestContextFilter.class)
    public GatewayRequestContextFilter gatewayContextFilter(@Autowired GatewayPluginProperties gatewayPluginProperties, @Autowired(required = false) ContextExtraDataGenerator contextExtraDataGenerator) {
        return new GatewayRequestContextFilter(gatewayPluginProperties, contextExtraDataGenerator);
    }

    /**
     * 缓存返回消息插件是否生效
     *
     * @return {}
     */
    @Bean
    @ConditionalOnMissingBean(GatewayResponseContextFilter.class)
    public GatewayResponseContextFilter responseLogFilter() {
        return new GatewayResponseContextFilter();
    }

    /**
     * 记录日志插件是否生效
     *
     * @return {}
     */
    @Bean
    @ConditionalOnMissingBean(RequestLogFilter.class)
    public RequestLogFilter requestLogFilter(@Autowired GatewayPluginProperties gatewayPluginProperties, CacheService cacheService) {
        return new RequestLogFilter(gatewayPluginProperties, cacheService);
    }

    /**
     * 清除缓存消息插件是否生效
     *
     * @return {}
     */
    @Bean
    @ConditionalOnBean(GatewayPluginProperties.class)
    @ConditionalOnMissingBean(RemoveGatewayContextFilter.class)
    public RemoveGatewayContextFilter removeGatewayContextFilter() {
        return new RemoveGatewayContextFilter();
    }

}
